package com.cn.goodsadmin.dao.impl;

import com.cn.goodsadmin.common.PageQueryResult;
import com.cn.goodsadmin.dao.BaseDao;
import com.cn.goodsadmin.dao.DestDTO;
import com.cn.goodsadmin.dao.IOrderDao;
import com.cn.goodsadmin.po.OrderInfo;
import com.cn.goodsadmin.util.TimeUtil;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate5.HibernateCallback;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by fly on 17/7/18.
 */
@Repository("orderDao")
public class OrderDaoImpl extends BaseDao<OrderInfo> implements IOrderDao {

    public List<OrderInfo> findOrderByOrderId(String orderId, String openid, Integer appid) throws Exception {
        StringBuilder builder = new StringBuilder();
        builder.append("from OrderInfo order where 1=1");
        if (orderId != null) {
            builder.append(" and order.cOrderid='" + orderId + "'");
        }
        if (openid != null) {
            builder.append(" and order.cOpenid='" + openid + "'");
        }
        if (appid != null) {
            builder.append(" and order.cAppid=" + appid);
        }
        List<OrderInfo> list = find(builder.toString());
        return list;
    }


    public List<OrderInfo> findFinishOrderByOpneid(String openid, int appid) throws Exception {
        StringBuilder builder = new StringBuilder();
        builder.append("from OrderInfo order where 1=1");
        if (openid != null) {
            builder.append(" and order.cOpenid='" + openid + "'");
        }
        builder.append(" and order.cAppid=" + appid);
        builder.append(" and order.cStatus=1");
        builder.append(" order by  order.cCtime desc ");
        List<OrderInfo> list = find(builder.toString());
        return list;
    }


    public PageQueryResult<DestDTO> getOrderInfoPage(String openid, int pageindex, int pagesize, int appid, int state) throws Exception {
        List<Object> paramsList = new ArrayList<Object>();
        StringBuilder builder = new StringBuilder();
        builder.append("from OrderInfo order where 1=1");
        int index=0;
        if (openid != null) {
            builder.append(" and order.cOpenid=?0");
            paramsList.add(openid);
            index++;
        }
        builder.append(" and order.cAppid=?"+index);
        paramsList.add(appid);
        index++;
        builder.append(" and order.cStatus=?"+index);
        paramsList.add((byte) state);
        builder.append(" order by  order.cCtime desc ");
        return this.commonQuery(builder.toString(), paramsList.toArray(), pageindex, pagesize, DestDTO.class);
    }

    public List<OrderInfo> findUserOrderByTime(final String openid, final Date btime, final Date etime, final int appid) throws Exception {
        final StringBuilder builder = new StringBuilder();
        builder.append("from OrderInfo order where ");
        builder.append(" order.cStatus=1 ");
        builder.append(" and order.cOpenid=?0");
        builder.append(" and date(order.cCtime)>=?1");
        builder.append(" and date(order.cCtime)<=?2");
        builder.append(" and order.cAppid=?3");
        return getHibernateTemplate().execute(new HibernateCallback<List<OrderInfo>>() {

            public List<OrderInfo> doInHibernate(Session session) throws HibernateException {
                Query query = session.createQuery(builder.toString());
                query.setParameter(0, openid);
                query.setParameter(1, btime);
                query.setParameter(2, etime);
                query.setParameter(3, appid);
                return query.list();
            }
        });
    }

    public List<OrderInfo> getAllOrderInfoByTime(final int day, final int status, final int appid) throws Exception {
        final StringBuilder builder = new StringBuilder();
        builder.append("from OrderInfo o where o.cStatus=?0");
        builder.append(" and o.cCtime>?1");
        builder.append(" and o.cCtime<?2");
        builder.append(" and o.cAppid=?3");
        return getHibernateTemplate().execute(new HibernateCallback<List<OrderInfo>>() {
            public List<OrderInfo> doInHibernate(Session session) throws HibernateException {
                Query query = session.createQuery(builder.toString());
                query.setParameter(0, (byte) status);
                query.setParameter(1, TimeUtil.getCurrDay(-1 * day));
                query.setParameter(2, TimeUtil.getCurrDay(0));
                query.setParameter(3, appid);
                return query.list();
            }
        });
    }

    @Override
    public List<OrderInfo> findUserLastOrder(String openid, int appid, int size) throws Exception {
        Criteria cri = getSessionFactory().getCurrentSession().createCriteria(OrderInfo.class);
        cri.add(Restrictions.eq("cOpenid", openid));
        cri.add(Restrictions.eq("cAppid", appid));
        cri.add(Restrictions.between("cStatus", (byte)1, (byte)3));
        cri.addOrder(Order.desc("cCtime"));
        cri.setMaxResults(size);
        List<OrderInfo> list = cri.list();
        return list;
    }
}
